package com.example.doctor.controller.IM;

import com.alibaba.fastjson.JSONObject;
import com.example.common.support.Result;
import com.example.common.utils.RequestHelp;
import com.example.doctor.dto.req.IMSaveReq;
import com.example.doctor.dto.req.SubmitRxReq;
import com.example.doctor.service.DoctorCenterService;
import com.example.doctor.service.InquiryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "问诊")
@RequestMapping("api/IM/Inquiry")
public class InquiryController {
    
    @Resource
    private InquiryService inquiryService;

    @Resource
    private DoctorCenterService doctorCenterService;

    @ApiOperation(value = "问诊订单")
    @PostMapping("/IMList")
    public Result<List<Map<String, Object>>> IMList(@RequestHeader Long UID){  
        return Result.success(inquiryService.IMList(UID));
    }  
    
    @ApiOperation(value = "问诊订单")
    @PostMapping("/NoReceiveList")
    public Result<List<Map<String, Object>>> NoReceiveList(@RequestHeader Long UID){  
        return Result.success(inquiryService.NoReceiveList(UID));
    }

    @ApiOperation(value = "接受问诊")
    @PostMapping("/Receive")
    public Result<Map<String, Object>> Receive(@RequestHeader Long UID,HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String OrderNO = Jo.getString("OrderNO");
        return Result.success(inquiryService.Receive(UID,OrderNO));
    }

    @ApiOperation(value = "患者基础信息")
    @PostMapping("/Patients")
    public Result<Map<String, Object>> Patients(@RequestHeader Long UID,HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String OrderNO = Jo.getString("OrderNO");
        return Result.success(inquiryService.Patients(UID,OrderNO));
    }

    @ApiOperation(value = "对话记录")
    @PostMapping("/IMLogList")
    public Result<List<Map<String, Object>>> IMLogList(HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        long OCID = Jo.getLongValue("OCID");
        int page = Jo.getIntValue("page");
        int limit = Jo.getIntValue("limit");
        return Result.success(inquiryService.IMLogList(OCID,page,limit));
    }

    @ApiOperation(value = "记录对话")
    @PostMapping("/IMSave")
    public Result<Map<String, Object>> IMSave(@RequestBody IMSaveReq req){          
        return Result.success(inquiryService.IMSave(req));
    }

    @ApiOperation(value = "上传对话文件")
    @PostMapping("/IMUpLoad")
    public Result<Object> IMUpLoad(@RequestParam(value = "file") MultipartFile file){          
        return Result.success(doctorCenterService.ImgUpLoad(file));
    }

    @ApiOperation(value = "结束问诊")
    @PostMapping("/IMFinish")
    public Result<Map<String, Object>> IMFinish(HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String OrderNO = Jo.getString("OrderNO");      
        return Result.success(inquiryService.IMFinish(OrderNO));
    }

    @ApiOperation(value = "提交用药建议")
    @PostMapping("/SubmitRx")
    public Result<Map<String, Object>> SubmitRx(@RequestHeader Long UID,@RequestBody SubmitRxReq req){  
        return Result.success(inquiryService.SubmitRx(UID, req));
    }

    @ApiOperation(value = "中药处方信息")
    @PostMapping("/ZRxDetail")
    public Result<Map<String, Object>> ZRxDetail(HttpServletRequest request){   
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String RxOrderNO = Jo.getString("RxOrderNO");      
        return Result.success(inquiryService.ZRxDetail(RxOrderNO));
    }

    @ApiOperation(value = "西药处方信息")
    @PostMapping("/XRxDetail")
    public Result<Map<String, Object>> XRxDetail(HttpServletRequest request){   
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String RxOrderNO = Jo.getString("RxOrderNO");      
        return Result.success(inquiryService.XRxDetail(RxOrderNO));
    }

    @ApiOperation(value = "问诊订单")
    @PostMapping("/IMOrderList")
    public Result<List<Map<String, Object>>> IMOrderList(@RequestHeader Long UID,HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        int page = Jo.getIntValue("page");
        int limit = Jo.getIntValue("limit"); 
        return Result.success(inquiryService.IMOrderList(UID,page,limit));
    }

    @ApiOperation(value = "问诊订单详情")
    @PostMapping("/IMOrderDetail")
    public Result<Map<String, Object>> IMOrderDetail(HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String OrderNO = Jo.getString("OrderNO");      
        return Result.success(inquiryService.IMOrderDetail(OrderNO));
    }

    @ApiOperation(value = "中医是否有舌像照")
    @PostMapping("/haveTongueImg")
    public Result<Boolean> haveTongueImg(HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String OrderNO = Jo.getString("OrderNO");      
        return Result.success(inquiryService.haveTongueImg(OrderNO));
    }

    //#region Pc
    @ApiOperation(value = "PC问诊列表", notes = "{\"beg\":\"2021-03-12 问诊开始日期\" ,\"end\":\"2021-03-21 问诊结束日期\",\"name\":\"患者\",\"aType\":10 在线复诊=10,    图文问诊=11,    电话问诊=12,    视频问诊=13,\"ocStatus\":-1  全部= -1  待接诊=0,    问诊中=1,    正常结束=2}")
    @PostMapping("/allIMList")
    public Result<List<Map<String, Object>>> allIMList(HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String name = Jo.getString("name");      
        Date beg = Jo.getDate("beg");   
        Date end = Jo.getDate("end"); 
        int ocStatus = Jo.getIntValue("ocStatus");
        int aType = Jo.getIntValue("aType");

        return Result.success(inquiryService.allIMList(beg, end, name, ocStatus,aType));
    }  

    @ApiOperation(value = "是否已CA签名", notes = "")
    @PostMapping("/isCaSess")
    public Result<Boolean> isCaSess(HttpServletRequest request){  
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String rxOrderNo = Jo.getString("rxOrderNo"); 

        return Result.success(inquiryService.isCaSess(rxOrderNo));
    }  

    @ApiOperation(value = "PcCA信息")
    @PostMapping("/getCaOrder")
    public Result<Map<String,Object>> getCaOrder(HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        String rxorderno = Jo.getString("rxOrderNo");
        return Result.success(inquiryService.getCaOrder(rxorderno));
    }
    //#endregion
}
